﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;

namespace XWord.WPF.ApplicationFramework
{
    public class BinaryFormatterHelper
    {
        public static void Serialize(string fileName, object obj)
        {
            if (obj == null) return;
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
            formatter.Serialize(stream, obj);
            stream.Dispose();
            stream = null;
            formatter = null;
        }

        private static int x;

        public static T Deserialize<T>(string fileName)
        {
            if (File.Exists(fileName))
            {

                IFormatter formatter = new BinaryFormatter();
                Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                var obj = (T)formatter.Deserialize(stream);
                stream.Dispose();
                stream = null;
                formatter = null;

                return obj;
            }
            return default(T);
        }

        public const string Path = "Data//";
    }
}
